package com.gdufe.mistakebook.controller;

import com.gdufe.mistakebook.entity.Label;
import com.gdufe.mistakebook.entity.LabelQuestion;
import com.gdufe.mistakebook.entity.dto.LabelDto;
import com.gdufe.mistakebook.exception.BusinessException;
import com.gdufe.mistakebook.mapper.LabelQuestionMapper;
import com.gdufe.mistakebook.result.PageResult;
import com.gdufe.mistakebook.result.Result;
import com.gdufe.mistakebook.service.LabelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("/label")
@Slf4j(topic = "LabelController.class")
@CrossOrigin
public class LabelController {
    @Autowired
    private LabelService labelService;

    @Resource
    private LabelQuestionMapper labelquestionMapper;

    /**
     * 条件分页查询错题
     *
     * @param params 查询参数
     * @return 分页结果
     */
    @RequestMapping ("/list")
    public Result<PageResult> listLabels(@RequestParam Map<String, String> params) {
        if (params == null) {
            throw new BusinessException(0, "参数不能为空");
        }
        PageResult pageResult = labelService.pageQuestions(params);
        return Result.success(pageResult);
    }

    /**
     * 查询标签
     */
    @GetMapping("/info/{id}")
    public Result<Label> getLabel(@PathVariable("id") Integer id){
        Label label = labelService.getLabelById(id);
        if(label!=null)
            return Result.success(label);
        else
            return Result.error("无该标签信息");
    }
    /**
     * 修改
     */
    @RequestMapping("/update")
    public Result<String> updateLabel(@RequestBody Label label) {
         labelService.updateLabel(label);
         return  Result.success("修改成功");
    }

    /**
     * 删改
     */
    @RequestMapping("/delete/{id}")
    public Result<String> deleteLabel(@PathVariable Integer id) {

        List<LabelQuestion> labelquestion = labelquestionMapper.getLabelQuestionsByLabelId(id);
        if(labelquestion.size()>0){
            throw new BusinessException(0, "该标签下存在题目，无法删除");

        }
        else {
            labelService.deleteLabel(id);
            return  Result.success("删除成功");
        }
    }
    /**
     * 批量删改
     */
    @RequestMapping("/delete")
    public Result<String> deleteLabels(@RequestBody Integer[] ids) {
        for (int i = 0; i < ids.length; i++) {
            List<LabelQuestion> labelquestion = labelquestionMapper.getLabelQuestionsByLabelId(ids[i]);
            if (labelquestion.size() > 0) {
                throw new BusinessException(0, "id为"+ ids[i] +"的标签下存在题目，无法删除");

            } else {
                labelService.deleteLabel(ids[i]);

            }
        }
        return Result.success("删除成功");
    }

    /**
     * 查询
     */
    @RequestMapping("/get")
    public Result<List<Map<String, String>>> listLabel(@RequestParam("userId") Integer userId){
        List<Map<String, String>> tagList = new ArrayList<>();
        tagList = labelService.getLabelNameId(userId);
        return  Result.success(tagList);
    }
    /**
     * 新增
     */
    @PostMapping("/save")
    public Result<String> InsertLabel(@RequestBody LabelDto labelDto){
        if (labelDto == null) {
            throw new BusinessException(0, "添加标签异常!");
        }
        log.warn("新增标签：{}", labelDto);
        labelService.insertLabel(labelDto);
        return Result.success("新增标签成功，快去看看吧!");
    }


}
